package com.supor.aiot.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;

import java.util.ArrayList;

import kotlin.jvm.Volatile;

/**
 * @author: zook
 * @date: 2021/12/31
 */
public class ScreenSiazeUtils {

    public int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    @Volatile
    public Point[] sRealSizes = new Point[2];

    public int getScreenRealHeight(Context context) {
        int orientation = context.getResources().getConfiguration().orientation;
        orientation = orientation == 1 ? 0 : 1;
        if (sRealSizes[orientation] == null) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getRealSize(point);
            sRealSizes[orientation] = point;
        }
        return sRealSizes[orientation].y;
    }

    public int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public int getScreenRealWidth(Context context) {
        int orientation = context.getResources().getConfiguration().orientation;
        orientation = orientation == 1 ? 0 : 1;
        if (sRealSizes[orientation] == null) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getRealSize(point);
            sRealSizes[orientation] = point;
        }
        return sRealSizes[orientation].x;
    }

//    public int getStatusBarHeight(Window window, Context context) {
//        Rect localRect = new Rect();
//        window.getDecorView().getWindowVisibleDisplayFrame(localRect);
//        int mStatusBarHeight = localRect.top;
//        if (0 == mStatusBarHeight) {
//            try {
//                Class<Object> localClass = Class.forName("com.android.internal.R\$dimen");
//                Object localObject = localClass.newInstance();
//                int i5 = localClass.getField("status_bar_height")[localObject].toString().toInt();
//                mStatusBarHeight = context.getResources().getDimensionPixelSize(i5);
//            } catch (ClassNotFoundException var6) {
//                var6.printStackTrace();
//            } catch (IllegalAccessException var7) {
//                var7.printStackTrace();
//            } catch (InstantiationException var8) {
//                var8.printStackTrace();
//            } catch (NumberFormatException var9) {
//                var9.printStackTrace();
//            } catch (IllegalArgumentException var10) {
//                var10.printStackTrace();
//            } catch (SecurityException var11) {
//                var11.printStackTrace();
//            } catch (NoSuchFieldException var12) {
//                var12.printStackTrace();
//            }
//        }
//        if (0 == mStatusBarHeight) {
//            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
//            if (resourceId > 0) {
//                mStatusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
//            }
//        }
//        return mStatusBarHeight;
//    }

    public int getNavigationBarHeight(Context context) {
        int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid != 0) {
            int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            return context.getResources().getDimensionPixelSize(resourceId);
        } else {
            return 0;
        }
    }

    public boolean getNavigationBarIsShow(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display display = context.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            Point realSize = new Point();
            display.getSize(size);  // app绘制区域
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
            boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);// 判断是否存在物理按键
            if (menu || back) {
                return false;
            } else {
                return true;
            }
        }
    }

    public boolean getNavigationBarIsShowWithRes(Activity context) {
        int id = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        // 判断系统是否写入了关于是否显示虚拟导航栏的相关变量,如果为true，表示有虚拟导航栏
        return id > 0 && context.getResources().getBoolean(id);
    }

    public boolean isAllScreenDevice(Context context) {
        if (Build.VERSION.SDK_INT < 21) {
            return false;
        } else {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getRealSize(point);
            float width;
            float height;
            if (point.x < point.y) {
                width = point.x;
                height = point.y;
            } else {
                width = point.y;
                height = point.x;
            }
            if (height / width >= 1.97f) {
                return true;
            }
            return false;
        }
    }

}
